import json, os
from cpgzh import *
from setup import *
from home import home
from classes import classes
from students import Students

from pgzero.loaders import root

pen = Pen()


def update():
    "更新数据"
    master.run_tasks()
    if master.status == 0:
        home.update()
    elif master.status == 1:
        classes.update()
    elif master.status == 2:
        students.update()


def draw():
    pen.clear()
    if master.status == 0:
        home.draw()
    elif master.status == 1:
        classes.draw()
    elif master.status == 2:
        students.draw()


def on_mouse_up(button, pos):
    """
    当鼠标抬起
    """
    global students
    lastPage = classes.lastPage
    nextPage = classes.nextPage
    if master.status == 0:
        if home.start.collidepoint_pixel(pos):
            home.start_game()
    elif master.status == 1:
        if lastPage.collidepoint_pixel(pos):
            classes.page -= 1
            if classes.page < 0:
                classes.page += len(classes.classes) // 4 + 1
        elif nextPage.collidepoint_pixel(pos):
            classes.page += 1
            if classes.page >= len(classes.classes) // 4 + 1:
                classes.page -= len(classes.classes) // 4 + 1
        else:
            for c in classes.classes[classes.page * 4 : (classes.page + 1) * 4]:
                if c.collidepoint_pixel(pos):
                    master.status = 2
                    data.classid = c.id
                    students = Students(data.classid, lastPage, nextPage)
                    break
    if master.status == 2:
        if lastPage.collidepoint_pixel(pos):
            students.page -= 1
            if students.page < 0:
                students.page += len(students.students) // 8 + 1
        elif nextPage.collidepoint_pixel(pos):
            students.page += 1
            if students.page >= len(students.students) // 8 + 1:
                students.page -= len(students.students) // 8 + 1
        elif students.actors[5].collidepoint_pixel(pos):
            if students.arm:
                arm = students.arm
                arm.pos = arm.x0, arm.y0
                mouse.show()
                students.arm = 0
        else:
            if students.arm:
                arm = students.arm
                for s in students.students:
                    if s.collide_pixel(arm) and s.show:
                        s.atk(arm.atk)
                        students.atkEffect()
                        for i in students.datas["students"]:
                            if s.id == i["id"]:
                                i["score"] = s.data["score"]
                                break
                        path = os.path.join(root, "datas.json")
                        with open(path, "w+", encoding="utf-8") as f:
                            json.dump(
                                students.datas,
                                f,
                                ensure_ascii=False,
                                indent=2,
                            )
                        break
            else:
                for arm in students.arms:
                    if arm.collidepoint_pixel(pos):
                        students.arm = arm
                        mouse.hide()
                        break


def on_mouse_move(pos):
    "当鼠标移动时"
    if master.status == 2:
        if students.arm:
            students.arm.pos = pos


def on_key_down(key):
    "按键"
    if key == keys.SPACE:
        master.status = 0
        home.start.scale = 0.7
        mouse.show()
        home.start.image = "开始游戏按钮-normal-000.png"


go()
